package com.pig4cloud.pig.module.controller.wmj;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.wmj.WmjDoorRecord;
import com.pig4cloud.pig.module.service.wmj.IWmjDoorRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 门禁出入记录Controller
 *
 * @author ruoyi
 * @date 2022-12-08
 */
@Validated
@Api(value = "门禁出入记录控制器", tags = {"门禁出入记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wmj/door/record")
public class WmjDoorRecordController {

    private final IWmjDoorRecordService wmjDoorRecordService;

    /**
     * 查询门禁出入记录列表
     */
    @ApiOperation("查询门禁出入记录列表")
    @PreAuthorize("@pms.hasPermission('module:wmjRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<WmjDoorRecord> list(@Validated WmjDoorRecord wmjDoorRecord) {
        QueryWrapper<WmjDoorRecord> qw = new QueryWrapper<>();
        if(wmjDoorRecord.getDoorId()!=null){
        	qw.eq("door_id",wmjDoorRecord.getDoorId());
		}

		if(StrUtil.isNotEmpty(wmjDoorRecord.getBeginDate())){
			qw.between("create_time",wmjDoorRecord.getBeginDate() + " 00:00:00",wmjDoorRecord.getEndDate() + " 23:59:59");
		}
        qw.orderByDesc("create_time");

        TableDataInfo<WmjDoorRecord> td = PageUtils.buildDataInfo(wmjDoorRecordService.page(PageUtils.buildPage(),qw));
        List<WmjDoorRecord> list = td.getData();
        return td;
    }

    /**
     * 导出门禁出入记录列表
     */
    @ApiOperation("导出门禁出入记录列表")
    @PreAuthorize("@pms.hasPermission('module:wmjRecord:export')")
    @SysLog("门禁出入记录")
    @GetMapping("/export")
    public List export(@Validated WmjDoorRecord wmjDoorRecord) {
        List<WmjDoorRecord> list = wmjDoorRecordService.list();
        // ExcelUtil<WmjDoorRecord> util = new // ExcelUtil<WmjDoorRecord>(WmjDoorRecord.class);
        return  list;// util.exportExcel(list, "门禁出入记录");
    }

    /**
     * 获取门禁出入记录详细信息
     */
    @ApiOperation("获取门禁出入记录详细信息")
    @PreAuthorize("@pms.hasPermission('module:wmjRecord:query')")
    @GetMapping("/{id}")
    public R<WmjDoorRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        WmjDoorRecord wmjDoorRecord = wmjDoorRecordService.getById(id);
        return R.ok(wmjDoorRecord);
    }

    /**
     * 新增门禁出入记录
     */
    @ApiOperation("新增门禁出入记录")
    @PreAuthorize("@pms.hasPermission('module:wmjRecord:add')")
    @SysLog("门禁出入记录")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody WmjDoorRecord wmjDoorRecord) {
        return R.okByRow(wmjDoorRecordService.save(wmjDoorRecord) ? 1 : 0);
    }

    /**
     * 修改门禁出入记录
     */
    @ApiOperation("修改门禁出入记录")
    @PreAuthorize("@pms.hasPermission('module:wmjRecord:edit')")
    @SysLog("门禁出入记录")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody WmjDoorRecord wmjDoorRecord) {
        return R.okByRow(wmjDoorRecordService.updateById(wmjDoorRecord) ? 1 : 0);
    }

    /**
     * 删除门禁出入记录
     */
    @ApiOperation("删除门禁出入记录")
    @PreAuthorize("@pms.hasPermission('module:wmjRecord:remove')")
    @SysLog("门禁出入记录")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(wmjDoorRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
